/*
* @Author: Ziwei Yang
* @Date:   2016-10-19
* @Last Modified by:   Ziwei Yang
* @Last Modified time: 2016-10-19
*/

'use strict';


var MapManager = function(){
	self = {};
	self.data = {};
	return self;
}
MapManager.exportMap = function(){
	var dataStr = "data:text/json;charset=utf-8," + encodeURIComponent(JSON.stringify(map));
	var downloadJson = document.getElementById('downloadJson');
	downloadJson.setAttribute("href",dataStr);
	downloadJson.setAttribute("download","map.json");
	downloadJson.click();
}
MapManager.importMap = function(){
	var importJson = document.getElementById('mapImport');
	importJson.click();
}
MapManager.mapLoad = function(target,obj){
	for(var i in obj){
		var column = obj[i];
		for(var k in column){
			target[i][k] = Terrain(column[k]);
		}
	}
}
MapManager.reset = function(){
	if(confirm('This will erase your current map!')){
		self.data = {};
		for(var i = 0; i < 20; i++){
			self.data['col'+i] = [];
			for(var k = 0; k < 20; k++){
				self.data['col'+i][k] = Terrain({
					x:25*k,
					y:25*i,
					width:25,
					height:25,
				});
			}
		}	
	}
}
MapManager.readJson = function(event) {
	var reader = new FileReader();
	reader.onload = function(event){
		var obj = JSON.parse(event.target.result);
		MapManager.mapLoad(self.data,obj.data);
	}
	if(!event.target.files[0]){
		console.log("File not detected");
	}else{
		reader.readAsText(event.target.files[0]);
	}
}
